package com.sm.healthy.model

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.sm.healthy.utils.CacheUtil
import com.blankj.utilcode.util.ToastUtils
import com.sm.healthy.base.App
import com.sm.healthy.data.bean.UserEntity
import com.sm.healthy.ext.gson
import kotlinx.coroutines.launch
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import me.hgj.jetpackmvvm.callback.livedata.BooleanLiveData
import me.hgj.jetpackmvvm.util.LogUtils


class UserCenterAddViewModel : BaseViewModel() {

    val tag = "UserCenterAddViewModel"
    val uid = CacheUtil.getCurUid()
    val dao = App.database.userDao()

    val userInfo = MutableLiveData<UserEntity>(UserEntity(uid))
    val isMan = BooleanLiveData(true)

    fun insert() {
        userInfo.value?.let {

            if (it.name.isEmpty()) {
                ToastUtils.showShort("请输入姓名")
                return@let
            }
            if (it.mobile.isEmpty()) {
                ToastUtils.showShort("请输入联系电话")
                return@let
            }

            LogUtils.debugInfo(tag, "data = ${gson.toJson(it)}")

            viewModelScope.launch {
                dao.insert(it)
            }
        }
    }

    fun update() {
        userInfo.value?.let {

            if (it.name.isEmpty()) {
                ToastUtils.showShort("请输入姓名")
                return@let
            }
            if (it.mobile.isEmpty()) {
                ToastUtils.showShort("请输入联系电话")
                return@let
            }

            LogUtils.debugInfo(tag, "data = ${gson.toJson(it)}")

            viewModelScope.launch {
                dao.update(it)
            }
        }
    }
}